<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">

          <el-input size="default" placeholder="请输入公司名" style="max-width: 180px" v-model="state.name">
          </el-input>


        <el-button size="default" type="primary" class="ml10" @click="getTableData()">
          <el-icon>
            <ele-Search />
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onOpenAdd('add')">
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          新增公司
        </el-button>
      </div>
      <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%"> <el-table-column
          prop="id" label="序号" show-overflow-tooltip width="60" align="center"></el-table-column>
        <el-table-column prop="name" label="公司名称" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column prop="money" label="公司金额" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column prop="tel" label="公司电话" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column prop="address" label="公司地址" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column prop="note" label="备注" show-overflow-tooltip align="center">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip align="center"></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary"
              @click="onOpenEditUser('edit', scope.row)">修改</el-button>
            <el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary"
              @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
        :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum" background
        v-model:page-size="state.tableData.param.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="state.tableData.total">
      </el-pagination>
    </el-card>
    <companyDialog ref="companyDialogFormRef" @refresh="getTableData()" />
  </div>
</template>

<script setup lang="ts" name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { get_company_list, del } from "/@/api/company/index";

// 引入组件
const companyDialog = defineAsyncComponent(() => import("/@/views/company/dialog.vue"));

// 定义变量内容
const companyDialogFormRef = ref();
const state = reactive({
  name: "",
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
    },
  },
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  get_company_list(
    state.name,
    state.tableData.param.pageNum,
    state.tableData.param.pageSize
  ).then((res) => {
    state.tableData.loading = false;
    state.tableData.data = res.data.records;
    state.tableData.total = res.data.total;
  });
};
// 打开新增用户弹窗
const onOpenAdd = (type: string) => {
  companyDialogFormRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, row: RowUserType) => {
  companyDialogFormRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row: any) => {
  ElMessageBox.confirm(`此操作将永久删除公司：“${row.name}”，是否继续?`, "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      del(row.id).then((res) => {
        if (res.code == 0) {
          ElMessage.success(res.msg);
          getTableData();
        }
      });
    })
    .catch(() => { });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
